package com.cmw.auntapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cmw.auntapp.R;
import com.cmw.auntapp.core.util.DateUtil;
import com.cmw.auntapp.model.OrderModel;
import com.cmw.auntapp.ui.OrderDetailActivity;
import com.cmw.auntapp.ui.OrderReceiveActivity;
import com.cmw.auntapp.ui.OrderServiceActivity;

import java.math.BigDecimal;
import java.util.List;

import static com.cmw.auntapp.constant.Constant.ORDER_XSTATUS_2;
import static com.cmw.auntapp.constant.Constant.ORDER_XSTATUS_SERVICE_IN;

/**
 * 订单列表数据源适配器
 *  * @Auth chengmingwei
 *  * @date 2021-04-19 23:11
 */
public class OrderListAdapter extends BaseAdapter {
    private static final String TAG = "OrderListAdapter";

    private List<OrderModel> orderList;

    private Context context;

    public OrderListAdapter(Context context, List<OrderModel> orderList) {
        this.context = context;
        this.orderList = orderList;
    }

    public void setOrderList(List<OrderModel> orderList) {
        this.orderList = orderList;
    }

    @Override
    public int getCount() {
        return (null != orderList) ? orderList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return orderList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return orderList.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) { // 如果没有复用
            convertView = View.inflate(context, R.layout.order_list_item, null);
        }
        OrderModel model = orderList.get(position);
        if (null == model) return convertView;
        TextView tvOtype = convertView.findViewById(R.id.tv_code);
        TextView tvCreateTime = convertView.findViewById(R.id.tv_create_time);
        TextView tvXstatus = convertView.findViewById(R.id.tv_amount);
        tvXstatus.setTag(position);
        tvXstatus.setOnClickListener((view -> goOrderDetail(view)));
        //tv_service_address
        TextView tvServiceAddress = convertView.findViewById(R.id.tv_service_address);
        //tv_val_totalAmount 应收金额
        TextView tvValTotalAmount = convertView.findViewById(R.id.tv_val_totalAmount);
        // tv_label_unint 元
        TextView tvLabelUnint = convertView.findViewById(R.id.tv_label_unint);

        Integer otype = model.getOtype();
        String otypeVal = "----";
        if (otype != null && otype.intValue() == 2) {
            otypeVal = "跑腿";
        } else {
            otypeVal = "钟点工";
        }
        tvOtype.setText(otypeVal);

        String createTimeVal = model.getCreateTime();
        createTimeVal = DateUtil.dateFormatToStr(DateUtil.DATE_FORMAT+" HH:mm",
                DateUtil.dateFormat(DateUtil.DATE_TIME_FORMAT2,createTimeVal));
        tvCreateTime.setText(createTimeVal);

        Integer xstatus = model.getXstatus();
        Object[] xstatusArr = getXstatusVal(xstatus);
        String xstatusVal = (String)xstatusArr[0];
        Integer xstatusColorResId = (Integer)xstatusArr[1];
        tvXstatus.setText(xstatusVal);
        tvXstatus.setTextColor(context.getResources().getColor(xstatusColorResId));

        String address = model.getAddress();
        tvServiceAddress.setText(address);

        BigDecimal payAmount = model.getPayAmount();
        if (payAmount == null) {
            payAmount = model.getTotalAmount();
        }
        if (payAmount == null) {
            payAmount = new BigDecimal("0");
        }
        tvValTotalAmount.setText(payAmount.toString());
        int colorResId = R.color.colorBlack;
        if (null != payAmount && payAmount.doubleValue() > 0) {
            colorResId = R.color.colorBlack;
        } else {
            colorResId = R.color.colorCCGray;
        }
        tvValTotalAmount.setTextColor(context.getResources().getColor(colorResId));
        tvLabelUnint.setTextColor(context.getResources().getColor(colorResId));
        return convertView;
    }

    public void goOrderDetail(View view) {
        int position = (int)view.getTag();
        Log.i(TAG, "goOrderDetail...position="+position);
        OrderModel model = orderList.get(position);
        if (null == model) {
            Log.e(TAG, "找不到索引位置："+position+"对应的订单信息!");
            return;
        }
        Long orderId = model.getId();
        Integer xstatus = model.getXstatus();
        Class targetActivity = OrderDetailActivity.class;
        switch (xstatus.intValue()) {
            case ORDER_XSTATUS_2:{
                targetActivity = OrderReceiveActivity.class;
                break;
            }case ORDER_XSTATUS_SERVICE_IN:{
                targetActivity = OrderServiceActivity.class;
                break;
            }
        }
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("orderId", orderId.toString());
        intent.putExtras(bundle);
        intent.setClass(context, targetActivity);
        context.startActivity(intent);
    }

    private Object[] getXstatusVal(Integer xstatus) {
        String xstatusVal = "";
        Integer textColor = R.color.black99;
        /**
         * 2:已接单,3:已取消(阿姨),4:服务中,5:服务完成,6:已支付，7:已结算(未评价),8:已结算（已评价）
         */
        switch (xstatus.intValue()) {
             case 0 : {
                xstatusVal = "待接单";
                textColor = R.color.colorCCGray;
                break;
            } case 1 : {
                xstatusVal = "客户已取消";
                textColor = R.color.colorCCGray;
                break;
            }case 2 : { // 已接单
                xstatusVal = "待服务";
                break;
            }  case 3 : { // 3:已取消(阿姨)
                xstatusVal = "已取消";
                textColor = R.color.colorCCGray;
                break;
            } case 4 : { // 4:服务中
                xstatusVal = "服务中";
                break;
            } case 5 : { // 5:服务完成
                xstatusVal = "待支付";
                textColor = R.color.colorRed;
                break;
            } case 6 : { // 6:已支付
                xstatusVal = "已支付";
                textColor = R.color.colorCCGray;
                break;
            }case 7 : { // 7:已结算(未评价)
                xstatusVal = "已评价";
                textColor = R.color.colorCCGray;
                break;
            }case 8 : { // 8:已结算(未评价)
                xstatusVal = "已结算(未评价)";
                textColor = R.color.colorCCGray;
                break;
            }case 9 : { // 9:已结算（已评价）
                xstatusVal = "已结算（已评价）";
                break;
            }
        }
        return new Object[]{xstatusVal, textColor};
    }
}
